package com.mifengs.mall.util;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;

import com.mifengs.mall.MallApplication;
import com.mifengs.mall.services.CountTimeService;

public class MyCountDownTime extends CountDownTimer {
    private Context mContext;
    public static final String RETRIEVE_CODE_MESSGE = "retrieve_message";
    public static final String REGISTER_CODE_MESSAGE = "register_message";
    private Intent countTimeReceiver;
    public String action;

    public MyCountDownTime(long millisInFuture, long countDownInterval, Context mContext, String action) {
        super(millisInFuture, countDownInterval);
        this.mContext = mContext;
        this.action = action;
        countTimeReceiver = new Intent(action);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if ("retrieveAction".equals(action)) {
            countTimeReceiver.putExtra(RETRIEVE_CODE_MESSGE, "获取验证码" + "\n" + ((millisUntilFinished / 1000 - 1) + "s"));
        } else if ("registerAction".equals(action)) {
            countTimeReceiver.putExtra(REGISTER_CODE_MESSAGE, "获取验证码" + "\n" + ((millisUntilFinished / 1000 - 1) + "s"));
        }
        mContext.sendBroadcast(countTimeReceiver);
    }

    @Override
    public void onFinish() {
        if ("retrieveAction".equals(action)) {
            countTimeReceiver.putExtra(RETRIEVE_CODE_MESSGE, "获取验证码");
        } else if ("registerAction".equals(action)) {
            countTimeReceiver.putExtra(REGISTER_CODE_MESSAGE, "获取验证码");
        }
        mContext.sendBroadcast(countTimeReceiver);
    }
}
